package com.fromfuture.xuhuidoc.android.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.fromfuture.xuhuidoc.R;
import com.fromfuture.xuhuidoc.android.base.BaseWebActivity;
import com.fromfuture.xuhuidoc.android.bean.MyConsultRoomRecycleViewInfo;
import com.fromfuture.xuhuidoc.android.callback.MyConsultRoomFra01ToFra02;
import com.fromfuture.xuhuidoc.android.callback.MyConsultRoomFragment02Fresh;
import com.fromfuture.xuhuidoc.android.commonresouce.MyUrl;
import com.fromfuture.xuhuidoc.android.customview.RoundImageView;
import com.fromfuture.xuhuidoc.cordova.callback.OnImageSuccessListener;
import com.fromfuture.xuhuidoc.cordova.plugin.MyConsultRoomPlugin;
import com.fromfuture.xuhuidoc.cordova.untils.CordovaAppManager;
import com.fromfuture.xuhuidoc.cordova.untils.ImageTask;

import java.util.List;

import app.auto.runner.base.utility.ToastUtil;

/**
 * Author：何送军
 * creation time：2017/8/25
 **/
public class MyConsultRoomAdapter extends RecyclerView.Adapter<MyConsultRoomAdapter.MyConsultRoomViewHolders>  {

    //内部接口
    MyConsultRoom_onItemClickListener onItemClickListener;
    MyConsultRoom_onItemLongClickListener onItemLongClickListener;
    MyConsultRoom_button_state_listener button_state_listener;
    MyConsultRoom_button_state_listener01 button_state_listener01;
    ToMyConsultRoom_02_Listener toMyConsultRoom_02_Listener;

    List<MyConsultRoomRecycleViewInfo>list;
    List<String >listItemInfo;
    List<Integer>list_isButtonState;
    private String tag="MyConsultRoomAdapter";
    Context context;
    int isAllButton=0;

    //外部接口
    MyConsultRoomFragment02Fresh myConsultRoomFragment02Fresh;
    MyConsultRoomFra01ToFra02 myConsultRoomFra01ToFra02;

    Handler handler=new Handler();

    public void setMyConsultRoomFragment02Fresh(MyConsultRoomFragment02Fresh myConsultRoomFragment02Fresh) {
        this.myConsultRoomFragment02Fresh = myConsultRoomFragment02Fresh;
    }

    private String patguid;

    public void changeButtonState(final int pos, int stata){
        if (list_isButtonState.size()!=0){
            list_isButtonState.set(pos,stata);
            handler.post(new Runnable() {
                @Override
                public void run() {
                    notifyItemChanged(pos);
                }
            });

        }

    }
    public void changeButtonState01(final int pos, int stata){
        if (list_isButtonState.size()!=0) {
            list_isButtonState.set(pos, stata);
            handler.post(new Runnable() {
                @Override
                public void run() {
                    notifyItemChanged(pos);
                }
            });

        }
    }
    public void setjiezhenPatGuid(String guid){
        this.patguid=guid;
    }



    /**
     *构造方法
     * */
    public MyConsultRoomAdapter(List<MyConsultRoomRecycleViewInfo> list, List<Integer>list_isButtonState,List<String>listItemInfo,Context context) {
        this.list = list;
        this.context = context;
        this.list_isButtonState=list_isButtonState;
        this.listItemInfo=listItemInfo;
        myConsultRoomFra01ToFra02= (MyConsultRoomFra01ToFra02) CordovaAppManager.myConsultRoomActivity.getFragmentManager().findFragmentByTag("myConsultRoomFragment02");
    }
    /**
     * set与get
     * */

    public MyConsultRoom_onItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(MyConsultRoom_onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public MyConsultRoom_onItemLongClickListener getOnItemLongClickListener() {
        return onItemLongClickListener;
    }

    public void setOnItemLongClickListener(MyConsultRoom_onItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public MyConsultRoom_button_state_listener getButton_state_listener() {
        return button_state_listener;
    }

    public void setButton_state_listener(MyConsultRoom_button_state_listener button_state_listener) {
        this.button_state_listener = button_state_listener;
    }


    public ToMyConsultRoom_02_Listener getToMyConsultRoom_02_Listener() {
        return toMyConsultRoom_02_Listener;
    }

    public void setToMyConsultRoom_02_Listener(ToMyConsultRoom_02_Listener toMyConsultRoom_02_Listener) {
        this.toMyConsultRoom_02_Listener = toMyConsultRoom_02_Listener;
    }

    /**
     * 重写
     * */
    @Override
    public MyConsultRoomViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.recycleview_my_consult_room_item, parent, false);
        return new MyConsultRoomViewHolders(view);
    }

    @Override
    public void onBindViewHolder(final MyConsultRoomViewHolders holder, final int position) {
        holder.name.setText(list.get(position).getConsultname());
        holder.time.setText(list.get(position).getConsulttime());
        holder.content.setText(list.get(position).getConsultcontents());
//final String im="https://sch.xmidware.com:443/chis/ROOT_CHIS/EAFormBlob.sp?guid=56711E529EED37B7E053AA0012AC8836";
        final String imgUrl=list.get(position).getConsultpic()+"&usrid=15581554261&userpwd=123456";
        final String imgUrl01= MyUrl.imgurl+imgUrl;
        holder.imageView.setImageResource(R.drawable.icon);

        ImageTask imageTask=new ImageTask(new OnImageSuccessListener() {
            @Override
            public void onImageSuccess(Bitmap bitmap) {
                holder.imageView.setImageBitmap(bitmap);
                Log.d("bitma的大小",bitmap.getByteCount()+"");
            }
        });
        imageTask.execute(imgUrl01);
        switch (list_isButtonState.get(position)){
            case 0:

                holder.button.setEnabled(true);
                holder.button.setText("接诊");
                holder.button.setBackgroundResource(R.drawable.button_shap01);
                holder.button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (isAllButton==0){
                            button_state_listener.button_state(position,1);
                            isAllButton=1;
                            myConsultRoomFragment02Fresh.fragment02Fresh();
                            //传值给fra02
                            Bundle bundle = new Bundle();
                            bundle.putString("content", listItemInfo.get(position));
                            myConsultRoomFra01ToFra02.setArgs(bundle);
                            toMyConsultRoom_02_Listener.toMyConsult();
                            MyConsultRoomPlugin.my_jiezhen(patguid,position);
                        }else if (isAllButton==1){
                            Toast.makeText(context,"有病人正在就诊",Toast.LENGTH_SHORT).show();
                        }

                    }
                });
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemClickListener.onItemClickListener(position,0);
                    }
                });
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        onItemLongClickListener.onItemLongClickListener(position,0);
                        return true;
                    }
                });

                break;
            case 1:
                holder.button.setEnabled(true);
                holder.button.setText("结束");
                holder.button.setBackgroundResource(R.drawable.button_shap02);
//                button_state_listener01.button_state(position,2);
                holder.button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        button_state_listener.button_state(position,2);
                        MyConsultRoomPlugin.docCloseOnePat(patguid);
                        isAllButton=0;
                    }
                });
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemClickListener.onItemClickListener(position,1);
                    }
                });
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        onItemLongClickListener.onItemLongClickListener(position,1);
                        return true;
                    }
                });


                break;
            case 2:
                holder.button.setEnabled(false);
                holder.button.setText("已结束");
                isAllButton=0;
                holder.button.setBackgroundColor(Color.parseColor("#00000000"));
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemClickListener.onItemClickListener(position,2);
                    }
                });
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        onItemLongClickListener.onItemLongClickListener(position,2);
                        return true;
                    }
                });
                break;
            case 3:
                holder.button.setEnabled(false);
                holder.button.setText("已退出");
                holder.button.setBackgroundColor(Color.parseColor("#00000000"));
                break;
        }


    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyConsultRoomViewHolders extends RecyclerView.ViewHolder{
        RoundImageView imageView;
        TextView name,time,content;
        TextView button;
        public MyConsultRoomViewHolders(View itemView) {
            super(itemView);
            imageView= (RoundImageView) itemView.findViewById(R.id.my_consult_room_01_item_img);
            name= (TextView) itemView.findViewById(R.id.my_consult_room_01_item_name);
            time= (TextView) itemView.findViewById(R.id.my_consult_room_01_item_time);
            content= (TextView) itemView.findViewById(R.id.my_consult_room_01_item_content);
            button= (TextView) itemView.findViewById(R.id.my_consult_room_01_item_btn);
        }

    }


    public interface MyConsultRoom_onItemClickListener {
        public void onItemClickListener(int pos, int stata);
    }

    public interface MyConsultRoom_onItemLongClickListener {
        public void onItemLongClickListener(int pos, int stasta);
    }

    //改变某个item的接诊状态
    public interface MyConsultRoom_button_state_listener{
        public void button_state(int pos, int state);
    }

    //改变某个item的接诊状态(患者退出诊室)
    public interface MyConsultRoom_button_state_listener01{
        public void button_state(int pos, int state);
    }

    public interface ToMyConsultRoom_02_Listener{
        public void toMyConsult();
    }



}
